import base64
import json
import time

import requests
from selenium import webdriver



# 验证码识别


driver = webdriver.Chrome()



driver.get('https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx')
code = driver.find_element_by_xpath('//*[@id="imgCode"]')
# 截取验证码图片
code.screenshot('./img/img_path.png')
def base64_api(uname, pwd, img, typeid):
    with open(img, 'rb') as f:
        base64_data = base64.b64encode(f.read())
        b64 = base64_data.decode()
    data = {"username": uname, "password": pwd, "typeid": typeid, "image": b64}
    result = json.loads(requests.post("http://api.ttshitu.com/predict", json=data).text)
    if result['success']:
        return result["data"]["result"]
    else:
        return result["message"]
    return ""

driver.find_element_by_xpath('//*[@id="email"]').send_keys('ea79shu@163.com')
time.sleep(2)

driver.find_element_by_xpath('//*[@id="pwd"]').send_keys('Dianfeng979')


result = base64_api(uname='18504743924', pwd='123456', img="./img/img_path.png", typeid=3)
time.sleep(2)

driver.find_element_by_xpath('//*[@id="code"]').send_keys(result)

driver.find_element_by_xpath('//*[@id="denglu"]').click()

time.sleep(1)
driver.find_element_by_xpath('//*[@id="txtKey"]').send_keys('沁园春')
driver.find_element_by_xpath('//*[@id="html"]/body/div[1]/div/div[2]/div[2]/div[1]/form/input[2]').click()

time.sleep(2)
driver.find_element_by_xpath('//*[@id="likeImg71250"]').click()

time.sleep(1)
driver.find_element_by_xpath('//*[@id="html"]/body/div[1]/div/div[2]/div[1]/a[6]').click()
